/*
 * Copyright 2009 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.deepsplink.interceptor {
	import org.splink.deepsplink.page.params.PageParameterUtil;
	import org.splink.library.IFinalizable;
	import org.splink.deepsplink.authentication.IAuthenticator;
	import org.splink.deepsplink.navigation.command.INavigationCommand;
	import org.splink.library.tree.INode;
	import org.splink.library.tree.Tree;
	import org.splink.library.utils.ArrayUtils;

	/**
	 * Redirects requests for protected pages and their descending pages to another page if
	 * the current user is not authorized to access the page.
	 * 
	 * @author Max Kugland
	 */
	final public class SecurityInterceptor implements IInterceptor, IFinalizable {
		private var _interceptIds : Array;
		private var _redirectId : String;
		private var _tree : Tree;
		private var _authenticationManager : IAuthenticator;
		private var _lastInterceptedId : String = "";
		private var _navigationCmd : INavigationCommand;

		/**
		 * @param redirectId the id of the <code>Page</code> to which added id's and their 
		 * descendantswill be redirected  
		 */
		public function SecurityInterceptor(navigationCmd : INavigationCommand, tree : Tree, redirectId : String) {
			_navigationCmd = navigationCmd;
			_tree = tree;
			_redirectId = redirectId;
			
			_interceptIds = new Array();
		}

		public function setAuthenticationManager(authenticationManager : IAuthenticator) : void {
			_authenticationManager = authenticationManager;
		}

		/**
		 * Adds a pageId and it's descendants to to the list of protected pages. Access to these pages 
		 * is restructed to authorized users. If an unauthorized user tries to access a protected page
		 * the request is intercepted and redirected
		 * 
		 * @param id an id which will be intercepted
		 */
		public function addProtectedId(id : String) : void {
			if(!ArrayUtils.contains(_interceptIds, id)) {
				_interceptIds.push(id);
				var chain : Array = Tree.getAllChildren(Tree.getNodeById(id, _tree.root));
				
				for each (var n : INode in chain) {
					_interceptIds.push(n.id);
				}
			}
		}

		/**
		 * Removes a page from the list of protected pages. All pages below the removed page are removed
		 * from the protected page list, too.
		 *  
		 * @param id the id of the page to remove
		 */
		public function removeProtectedId(id : String) : void {
			ArrayUtils.remove(_interceptIds, id);
			var chain : Array = Tree.getAllChildren(Tree.getNodeById(id, _tree.root));
			
			for each (var n : INode in chain) {
				ArrayUtils.remove(_interceptIds, n.id);
			}
		}

		/**
		 * Carries out the interception by checking whether the user is authenticated and whether
		 * the page the user tries to access is protected. If the user is not authenticated and the
		 * page is protected, the user is redirected to the page corresponding to the configured
		 * protected page redirect id.
		 * 
		 * @param id the id which might be intercepted
		 * @return the result of the interception
		 */
		public function intercept(id : String, pageParams : Array) : Boolean {
			if(_authenticationManager.isAuthenticated()) {
				return false;
			}
			
			for each (var interceptId : String in _interceptIds) {
				if(id == interceptId) {
					_lastInterceptedId = PageParameterUtil.constructId(id, pageParams);
					_navigationCmd.gotoPage(_redirectId);
					return true;
				}
			}
			return false;
		}

		/**
		 * Finalizes the interceptor
		 */
		public function finalize() : void {
			_interceptIds = null;
			_tree = null;
		}

		/**
		 * @inheritDoc
		 */
		public function get lastInterceptedId() : String {
			return _lastInterceptedId;
		}
	}
}